package com.cosc716.restaurant.domain;

import java.util.ArrayList;
import java.util.Iterator;

public class Tabs {

	private ArrayList<Tab> tabs;
	
	public Tabs() {
		tabs = new ArrayList<Tab>();
	}
	
	public void addTab(Tab tab) {
		tabs.add(tab);
	}
	
	public ArrayList<Tab> getTabs() {
		return tabs;
	}
	
	public static TableOpenTabIterator
		getTableOpenTabIterator(Tabs tabs, Table table) {
		return new TableOpenTabIterator(tabs, table);
	}
	
	public TableOpenTabIterator getTableOpenTabIterator(Table table) {
		return getTableOpenTabIterator(this, table);
	}
	
	public static CustomerOpenTabIterator
		getCustomerOpenTabIterator(Tabs tabs, Customer customer) {
		return new CustomerOpenTabIterator(tabs, customer);
	}
	
	public CustomerOpenTabIterator getCustomerOpenTabIterator(Customer customer) {
		return getCustomerOpenTabIterator(this, customer);
	}

}

class TableOpenTabIterator implements Iterator<Tab> {

	private ArrayList<Tab> tabs = null;
	private int position = 0;
	private Table table = null;
	
	public TableOpenTabIterator(Tabs tabs, Table table) {
		this.tabs = tabs.getTabs();
		this.table = table;
	}
	
	@Override
	public boolean hasNext() {
		while(position < tabs.size()) {
			if( (tabs.get(position).getTable().getTableNumber() ==
					table.getTableNumber()) &&
				(!tabs.get(position).isPaid())	
					) {
				return true;
			}
			position++;
		}
		
		return false;
	}

	@Override
	public Tab next() {
		return tabs.get(position++);
	}

	@Override
	public void remove() {
		tabs.remove(position);
	}
	
}

class CustomerOpenTabIterator implements Iterator<Tab> {

	private ArrayList<Tab> tabs;
	private int position = 0;
	private Customer customer = null;

	public CustomerOpenTabIterator(Tabs tabs, Customer customer) {
		this.tabs = tabs.getTabs();
		this.customer = customer;
	}
	
	@Override
	public boolean hasNext() {
		while(position < tabs.size()) {
			if( (tabs.get(position).getCustomer().getCustomerId().
					equals(customer.getCustomerId())) &&
				(!tabs.get(position).isPaid())	
					) {
				return true;
			}
			position++;
		}
		
		return false;
	}

	@Override
	public Tab next() {
		return tabs.get(position++);
	}

	@Override
	public void remove() {
		tabs.remove(position);
	}
}